
import Mock from "mockjs";
let mockData = Mock.mock({
    'list|200': [ // 生成 200 条数据
        {
            id: () => Mock.Random.guid(), // ID 从 1 开始递增
            name: '@cname', // 随机中文姓名
            address: '@county(true)', // 随机地址
            'age|18-60': 1, // 随机年龄
            birth: '@date', // 随机出生日期
            'sex|1': ['0', '1'] // 随机性别
        }
    ]
}).list;
export default [
    {
        url: '/api/tabel/list',
        method: 'get',
        response: () => {
            return {
                code: 0,
                msg: 'ok',
                data: [
                    {
                        props: 'name',
                        label: '姓名'
                    },
                    {
                        props: 'age',
                        label: '年龄'
                    },
                    {
                        props: 'sex',
                        label: '性别'
                    },
                    {
                        props: 'birth',
                        label: '生日',
                        width: 200
                    },
                    {
                        props: 'address',
                        label: '地址',
                        width: 320

                    },
                ]
            }
        }
    },
    {
        url: '/api/user/list',
        method: 'get',
        response: ({ query }) => {
            let newArr = [];
            let page = query.page || 1;
            let name = query.name; // 获取查询参数中的 name
            const limit = 20
            const num = page * query.size
            for (let i = page-1; i < mockData.length; i++) {
                console.log(i);
                if (mockData[i] != undefined) {
                    // 根据查询条件筛选数据
                    if (name && !mockData[i].name.indexOf(name)) {
                        newArr.push(mockData[i]);
                    }
                    if (!name) {
                        newArr = mockData;
                    }
                }
            }
            console.log(newArr);
            // const pageList = newArr.filter((item, index) => index < limit * page && index >= limit * (page - 1))
            const pageSize = 10; // 每页 10 条数据
            const start = (page - 1) * pageSize;
            const end = start + pageSize;
            const paginatedList = newArr.slice(start, end);
            return {
                code: 0,
                msg: 'ok',
                data: {
                    total: mockData.length, // 返回筛选后的总条数
                    list: paginatedList // 返回分页后的数据
                }
            };
        }
    },
    {
        url: '/api/user/add',
        method: 'POST',
        response: ({ body }) => {
            const { name, age, sex, birth, address } = body;
            mockData.unshift({
                id: Mock.Random.guid(),
                name: name,
                age: age,
                sex: sex,
                birth: birth,
                address: address,
            })
            return {
                code: 200,
                data: {
                    message: '添加成功'
                }
            }
        }
    }, {
        url: '/api/user/edit',
        method: 'post',
        response: ({ body }) => {
            const { id, name, age, sex, birth, address } = body;
            const sex_int = parseInt(sex);
            mockData.some(u => {
                console.log(u.id, id);
                if (u.id === id) {
                    console.log('00000');
                    u.name = name;
                    u.age = age;
                    u.sex = sex_int;
                    u.birth = birth;
                    u.address = address;
                    return true;
                }
            })
            return {
                code: 200,
                msg: 'ok',
                data: {
                    message: '修改成功',
                    data: mockData
                }
            }
        }
    },
    {
        url: '/api/user/del',
        method: 'get',
        response: ({ query }) => {
            console.log('100');
            const id = query.id
            mockData = mockData.filter(u => u.id !== id)
            return {
                code: 200,
                message: '删除成功',
            }
        }
    }
]